﻿using PacoToolKit.Format;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Protocol.Flowmeter
{
    public static class Encoder
    {
        /// <summary>
        /// 标准Modbus组帧
        /// </summary>
        /// <param name="addr">从机地址 01-99</param>
        /// <param name="funcCode">功能码 eg 0x03</param>
        /// <param name="di">寄存器首址(高在前低在后) eg 1012</param>
        /// <param name="length">长度</param>
        /// <returns></returns>
        public static string EncodeModbusMsg(string addr, string funcCode, string di, int length)
        {
            string len = Formater.DECtoHEX(length.ToString(), 4);
            string str = addr + funcCode + di + len;
            string ret = Formater.BytesToHexStr(Formater.crc16(Formater.HexStringToBytesKeep(str)));//计算好crc加到尾部
            return ret;
        }

        /// <summary>
        /// 红外测温传感器组帧
        /// </summary>
        /// <param name="addr">Hex从机地址00-FF</param>
        /// <returns></returns>
        public static string EncodeInfraredTemSensorMsg(string addr)
        {
            string header = "5450";
            string code = "F1";
            string str = header + addr + code;
            string csc = Formater.CalcCSCode(str);
            return str + csc;
        }
    }
}
